﻿
using System;
using System.Collections.Generic;
using System.Text;

using YouHP.Utility;
using YouHP.BLL;
using YouHP.Model;
using YouHP.Model.Group;
using System.Web;

namespace YouHP.Pages.User.Group
{
    public class Index : YouHP.Pages.User.UserBasePage
    {
        protected GroupInfo GroupData { get; set; }
        protected List<GroupInfo> GroupListCreat { get; set; }
        protected List<GroupInfo> GroupListJoin { get; set; }
        protected List<GroupInfo> MyGroupList { get; set; }
        protected int pageSize = 5;
        protected int pageIndex = 1;
        protected int pageCount = 0;
        protected int groupCount;
        protected string PagingString;
   

        protected override void ShowPage()
        {
            base.ShowPage();
            if (ErrorCount > 0) return;
            this.AddLocation("用户管理中心", "/User/Index.aspx");
            this.AddLocation("圈子管理", "/User/Group/Index.aspx"); 

            HandleCommon();
            if (IsPost) HandlePost();
            else HandleGet();
        }

        protected void HandleCommon()
        {

        }

        protected void HandlePost()
        {
            int formid = RequestTools.GetFormInt("formid", 0);
            int typeid = RequestTools.GetFormInt("typeid", 0);
            int groupid = RequestTools.GetFormInt("groupid", 0);
            int role = RequestTools.GetFormInt("grouprole", -100);
            if (typeid == 1 && formid == 1 && groupid > 0 && role != -100)
            {
                bool succ = false;
                bool succ2 = false;
                bool succ3 = false;
                if (role == 2)//如果是圈主，使圈子无效，并将角色置为已删除
                {
                    succ = GroupBLL.Instance.InvalidateGroup(groupid);
                    succ2 = GroupUserBLL.Instance.UpdateGroupUserRole(groupid, UserData.Id, Constants.RoleDeleted);
                    if (succ && succ2)
                    {
                        this.AddMsgLine("退出成功");
                        this.SetUrl("/User/Group/index.aspx");
                        this.SetMetaRefresh();
                    }
                    else
                    {
                        this.AddErrLine("退出失败");
                    }

                }
                else
                {

                    succ3 = GroupUserBLL.Instance.UpdateGroupUserRole(groupid, UserData.Id, Constants.RoleDeleted);
                    if (succ3)
                    {
                        this.AddMsgLine("退出成功");
                        this.SetUrl("/User/Group/index.aspx");
                        this.SetMetaRefresh();
                    }
                    else
                    {
                        this.AddErrLine("退出失败");
                    }
                }

    
            }
            else
            {
                this.AddErrLine("退出失败");
            }
        }

        protected void HandleGet()
        {

            pageIndex = RequestTools.GetQueryInt("page", 1);
            MyGroupList = GroupBLL.Instance.GetAllGroupsByUserId(pageSize, pageIndex, UserData.Id, out groupCount);

            pageCount = Utils.GetPageCount(groupCount, pageSize);

            string query = HttpContext.Current.Request.Url.Query;
            query = Utils.RemoveQueryItem(query, "page");

            PagingString = this.GetPagingString(pageIndex, pageCount,
                "/User/Group/Index.aspx" + query, "page");


        }
    }
}